package mvc.controller;

import mvc.model.pojos.PictureBean;
import mvc.model.service.ProductService;
import mvc.model.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ColorPicServlet",urlPatterns = "colorpic")
public class ColorPicServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("cid");
        String pname = req.getParameter("pname");
        int cid = Integer.parseInt(id);
        ProductService productService = new ProductServiceImpl();
        //通过cid和pname查出对应颜色的图片
        List<PictureBean> pictures = productService.queryPic(cid,pname);
        //取出第一张图路径作为大图
        String firstPic =pictures.get(0).getPath();
        req.getSession().setAttribute("firstPic",firstPic);
        req.getSession().setAttribute("pictures",pictures);
    }
}
